package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.wb.dao.WbNoticeDao;
import com.cyt.work.wb.model.po.WbNotice;
import com.cyt.work.wb.model.vo.WbNoticeVo;
import com.cyt.work.wb.service.WbNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维保模块-通知公告表Service层实现类
 *
 * @author dingxu
 * @date 2019-12-6 8:18:44
 */
@Service("wbNoticeService")
public class WbNoticeServiceImpl extends BaseServiceImpl<WbNotice> implements WbNoticeService {

    @Resource
    private WbNoticeDao wbNoticeDao;

    @Autowired
    public void setBaseDao(WbNoticeDao wbNoticeDao) {
        this.baseDao = wbNoticeDao;
    }
    /**
     * 查询对象集合前几条数据
     *
     * @param wbNoticeVo 对象
     * @param num 几条数据
     * @return List 对象集合
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    public List<WbNotice> listExampleLimit(WbNoticeVo wbNoticeVo,int num) {
        Example example = new Example(WbNotice.class);
        example.setOrderByClause(" create_time desc limit 0,"+num);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(wbNoticeVo.getTitle())) {
            criteria.andLike("title", "%" + wbNoticeVo.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(wbNoticeVo.getApplyId())) {
            criteria.andEqualTo("applyId",  wbNoticeVo.getApplyId());
        }
        return wbNoticeDao.selectByExample(example);
    }
    /**
     * 查询对象集合
     *
     * @param wbNoticeVo 对象
     * @return List 对象集合
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    public List<WbNotice> listExample(WbNoticeVo wbNoticeVo) {
        Example example = new Example(WbNotice.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(wbNoticeVo.getTitle())) {
            criteria.andLike("title", "%" + wbNoticeVo.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(wbNoticeVo.getApplyId())) {
            criteria.andEqualTo("applyId",  wbNoticeVo.getApplyId());
        }
        return wbNoticeDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbNoticeVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    public ReturnResult page(WbNoticeVo wbNoticeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbNotice> list = listExample(wbNoticeVo);
        PageInfo<WbNotice> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbNoticeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbNoticeVo wbNoticeVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbNoticeVo.getId())) {
            WbNotice wbNotice = new WbNotice(sysUser);
            wbNotice.setApplyId(wbNoticeVo.getApplyId());
            wbNotice.setTitle(wbNoticeVo.getTitle());
            wbNotice.setContent(wbNoticeVo.getContent());
            wbNotice.setSort(Integer.parseInt(wbNoticeVo.getSort()));
            wbNoticeDao.insert(wbNotice);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbNotice wbNotice = wbNoticeDao.selectByPrimaryKey(wbNoticeVo.getId());
            wbNotice.setApplyId(wbNoticeVo.getApplyId());
            wbNotice.setTitle(wbNoticeVo.getTitle());
            wbNotice.setContent(wbNoticeVo.getContent());
            wbNotice.setSort(Integer.parseInt(wbNoticeVo.getSort()));
            wbNotice.setUpdateTime(new Date());
            wbNotice.setUpdateUser(sysUser.getId());
            wbNoticeDao.updateByPrimaryKey(wbNotice);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbNoticeVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbNoticeVo wbNoticeVo, SysUser sysUser) {
        //要修改的字段
        WbNotice wbNotice = new WbNotice();
        wbNotice.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbNotice.setUpdateUser(sysUser.getId());
        wbNotice.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbNotice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbNoticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbNoticeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbNoticeDao.updateByExampleSelective(wbNotice, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbNoticeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-6 8:18:44
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbNoticeVo wbNoticeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbNotice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbNoticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbNoticeVo.getId().split(",")));
        }
        wbNoticeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}